<?php
/**
 * 工程基类
 * @since   2017/02/28 创建
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */

namespace app\api\controller;

use app\command\controller\Generate;
use app\util\ReturnCode;
use think\cache\driver\Redis;
use think\Controller;
use app\command\controller\Testing;

//print_r(date("Y-m-d H:i:s",strtotime(20191229170357)));exit(); //表中YmdHis的反转义
class Base extends Controller {

    public $userID;
    private $debug = [];
    protected $userInfo = [];
    protected $openIDUrl = "https://api.weixin.qq.com/sns/jscode2session";

    public function _initialize() {
//        $this->userInfo = ''; 这部分初始化用户信息可以参考admin模块下的Base去自行处理
    }
    public function __construct(){
        parent::__construct();
    }

    /**
     * 用户token
     * @param $id
     * @param string $param
     * @return string
     */
    public function userToken($id, $param = ""){
        $token = Generate::createToken($id,$param);
        if($token){
            $redis = new Redis();
            if($redis->has($token)){
                $this->userToken($id,$param);
            }
            $redis->set($token, $id, 600);
        }
        return $token;
    }
    public function buildSuccess($data = [], $msg = '操作成功', $code = ReturnCode::SUCCESS) {
        $return = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        if (config('app.app_debug') && $this->debug) {
            $return['debug'] = $this->debug;
        }

        return $return;
    }

    public function buildFailed($code, $msg = '操作失败', $data = []) {
        $return = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        if (config('app.app_debug') && $this->debug) {
            $return['debug'] = $this->debug;
        }

        return $return;
    }

    protected function debug($data) {
        if ($data) {
            $this->debug[] = $data;
        }
    }
}
